
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px"
             :inline="true" label-position="left">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="籍贯"></el-input>
    </el-form-item>
    <el-form-item label="民族" prop="minzu">
      <el-select v-model="dataForm.minzu" placeholder="请选择民族" filterable>
        <el-option
          v-for="item in minzuList1"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-select v-model="dataForm.gender" placeholder="请选择性别">
        <el-option
          v-for="item in genderList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="工龄" prop="workYear">
      <el-select v-model="dataForm.workYear" placeholder="请选择工龄">
        <el-option
          v-for="item in workYearList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="预约时间" prop="appointmentTime">
      <el-date-picker
        v-model="dataForm.appointmentTime"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="选择预约时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="简历" v-model="dataForm.userResume">
      <el-upload drag :action="url1" :on-success="successHandle" file >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传pdf文件，且不超过10mb</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="应聘岗位" prop="station">
      <el-select v-model="dataForm.station" filterable placeholder="请选择应聘岗位">
        <el-option
          v-for="item in stationList1"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="面试官" prop="interviewer">
      <el-select v-model="dataForm.interviewer" placeholder="请选择面试官">
        <el-option
          v-for="item in interviewerList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        url1:'',
        visible: false,
        minzuList1: [],
        stationList1:[],
        workYearList:[
          {
            value: 0,
            label: '无工龄'
          },{
            value: 1,
            label: '一年'
          },{
            value: 2,
            label: '两年'
          },{
            value: 3,
            label: '三年'
          },{
            value: 4,
            label: '三年以上'
          }
        ],
        genderList:[{
          value: 1,
          label: '男'
        },{
          value: 0,
          label: '女'
        }],
        interviewerList:[],
        dataForm: {
          id: 0,
          name: '',
          phone: '',
          nativePlace: '',
          minzu: '',
          age: '',
          gender: '',
          workYear: '',
          appointmentTime: '',
          userResume: '',
          station: '',
          interviewer: '',
        },
        dataRule: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' },
            { message: '手机号格式错误', trigger: 'blur' , pattern: /^1[3-9]\d{9}$/ }
          ],
          nativePlace: [
            { required: true, message: '籍贯不能为空', trigger: 'blur' }
          ],
          minzu: [
            { required: true, message: '民族不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别 (0：女 1：男）不能为空', trigger: 'blur' }
          ],
          workYear: [
            { required: true, message: '工龄不能为空', trigger: 'blur' }
          ],
          appointmentTime: [
            { required: true, message: '预约时间不能为空', trigger: 'blur' }
          ],
          station: [
            { required: true, message: '应聘岗位不能为空', trigger: 'blur' }
          ],
          interviewer: [
            { required: true, message: '面试官不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getInterviewer();
      this.getMinzuList();
      this.getStationList();
    },
    methods: {
      init (id) {
        this.url1 = this.$http.adornUrl(`/file/upload5?token=${this.$cookie.get('token')}`)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/interview/interviewRecords/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.interviewRecords.name
                this.dataForm.phone = data.interviewRecords.phone
                this.dataForm.nativePlace = data.interviewRecords.nativePlace
                this.dataForm.minzu = data.interviewRecords.minzu
                this.dataForm.age = data.interviewRecords.age
                this.dataForm.gender = data.interviewRecords.gender
                this.dataForm.workYear = data.interviewRecords.workYear
                this.dataForm.appointmentTime = data.interviewRecords.appointmentTime
                this.dataForm.userResume = data.interviewRecords.userResume
                this.dataForm.station = data.interviewRecords.station
                this.dataForm.interviewer = data.interviewRecords.interviewer
              }
            })
          }
        })
      },
      // 上传成功
      successHandle (response) {
        console.log(response.assetsImg)
        this.dataForm.userResume = response.assetsImg
        this.$message.success('上传成功')
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/interview/interviewRecords/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'nativePlace': this.dataForm.nativePlace,
                'minzu': this.dataForm.minzu,
                'age': this.dataForm.age,
                'gender': this.dataForm.gender,
                'workYear': this.dataForm.workYear,
                'appointmentTime': this.dataForm.appointmentTime,
                'userResume': this.dataForm.userResume,
                'station': this.dataForm.station,
                'interviewer': this.dataForm.interviewer,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取面试官姓名集合
      getInterviewer(){
        this.$http({
          url: this.$http.adornUrl(`/sys/user/getInterviewer`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.interviewerList = data.interviewers.map(item => ({
              value: item.userId,
              label: item.username
            }))
            console.log(this.interviewerList)
          }
        })
      },
      // 获取所有民族的信息
      getMinzuList(){
        this.$http({
          url: this.$http.adornUrl(`/interview/minzu/getMinzuList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.minzuList1 = data.minzuList.map(item => ({
              value: item.minzuCode,
              label: item.minzuName
            }))
          }
        })
      },
      // 获取所有招聘岗位的信息
      getStationList(){
        this.$http({
          url: this.$http.adornUrl(`/interview/station/getStationList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.stationList1 = data.stationList.map(item => ({
              value: item.stationCode,
              label: item.stationName
            }))
            console.log(this.stationList1)
          }
        })
      }

    }
  }
</script>


